package homework01;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.Scanner;

/*有五个学生，每个学生有3门课的成绩，
从键盘输入以上数据（包括学生号，姓名，三门课成绩），
计算出平均成绩，况原有的数据和计算出的平均分数存放在磁盘文件 "stud.txt "中。*/
public class FiveStudent01 {
    static ArrayList<Student01>list=new ArrayList<Student01>();
    static BufferedWriter bw=null;
    public static void main(String[] args) {

        }
//从键盘获取学生信息
    public static void getStudent(){
        Scanner sc=new Scanner(System.in);
        int i=1;
        while (list.size()<3){
            System.out.println("请输入学生成绩：");
            System.out.println("请输入第"+i+"个学生的编号");
            int id=sc.nextInt();
            System.out.println("请输入第"+i+"个学生的姓名");
            String name=sc.next();
            System.out.println("请输入第"+i+"个学生的java成绩");
            int sj=sc.nextInt();
            System.out.println("请输入第"+i+"个学生的数据库成绩");
            int ss=sc.nextInt();
            System.out.println("请输入第"+i+"个学生的web成绩");
            int sw=sc.nextInt();
            Student01 s=new Student01();
            if (!(sj<0)&&(ss<0)&&(sw<0)){//判断学生成绩输入是否合法
                s.setId(id);
                s.setName(name);
                s.setScore1(sj);
                s.setScore2(ss);
                s.setScore3(sw);
                s.setAvg(StudentAvgScore.getAvg(s));
                list.add(s);
                i++;
            }else {
                System.out.println("成绩输入不合法！");
                continue;
            }
        }
        System.out.println(list);
    }
    //将学生信息录入文件
    public static void save(){
        try {
            bw=new BufferedWriter(new FileWriter("stud.txt"));
            for (int i=0;i<list.size();i++){
                bw.write("姓名："+list.get(i).getName()+"学号："+
                        list.get(i).getName()+"java成绩："+list.get(i).getScore1()+
                        "数据库成绩："+list.get(i).getScore2()+"web成绩："+list.get(i).getScore3());
                bw.newLine();
            }
        }catch (IOException e){
            System.out.println(e);
        }finally {
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    }
 class Student01{//学生信息类
    private int id;//学号
    private String name;//姓名
     private  int score1;//Java成绩
     private int score2;//数据库成绩
     private int score3;//Web成绩
     private int avg;
     public Student01() {
     }
     public Student01(int id, String name, int score1, int score2, int score3) {
         this.id = id;
         this.name = name;
         this.score1 = score1;
         this.score2 = score2;
         this.score3 = score3;
     }
     public int getId() {
         return this.id;
     }
     public void setId(int id) {
         this.id = id;
     }
     public String getName() {
         return this.name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public int getScore1() {
         return score1;
     }
     public void setScore1(int score1) {
         this.score1 = score1;
     }
     public int getScore2() {
         return score2;
     }
     public void setScore2(int score2) {
         this.score2 = score2;
     }
     public int getScore3() {
         return score3;
     }
     public void setScore3(int score3) {
         this.score3 = score3;
     }

     public int getAvg() {
         return avg;
     }

     public void setAvg(int avg) {
         this.avg = avg;
     }

     @Override
     public String toString() {
         return "学号" + id +
                 ", 姓名" + name + '\'' +
                 ", java成绩=" + score1 +
                 ", 数据库成绩=" + score2 +
                 ", web成绩=" + score3 +
                 '}';
     }
 }
 class StudentAvgScore{//获取成绩平均分
    static Student01 st01;
    static int avg;
    public static int getAvg(Student01 st1){
        int sjava;
        int ssql;
        int sweb;
        st01=st1;
        sjava=st01.getScore1();
        ssql=st01.getScore2();
        sweb=st01.getScore3();
        avg=(sjava+ssql+sweb)/3;
        return avg;
    }
 }


